package com.tzy.note.base.thread.create;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * 创建线程方式三：
 *      1.实现Callable接口
 *      2.重写call方法
 *      3.通过上面类的对象 new FutureTask对象
 *      4.通过FutureTask对象 new Thread对象
 *      5.Thread对象调用start方法
 *
 */
public class CreateThread3 implements Callable {
    @Override
    public Integer call() throws Exception {
        int result = 0;
        for (int i = 1; i <= 100; i++) {
            result = result + i;
        }
        System.out.println("线程: " + Thread.currentThread().getName() + " 在做数学题！");
        return result;
    }
    public static void main(String[] args) throws Exception {
        FutureTask<Integer> futureTask = new FutureTask<>(new CreateThread3());
        new Thread(futureTask).start();
        //阻塞等待整个线程执行完成，获取返回结果
        Integer result = futureTask.get();
        System.out.println("线程: " + Thread.currentThread().getName() + " 得到计算结果" + result);
    }
}
